<template>
  <div class="box-all">
    <div class="content-box w-card">
      <div class="title-box">
        <span>推荐人管理</span>
        <el-button style="margin-left:auto;" @click="addAccount(1)">新增推荐人</el-button>
      </div>
      <div class="search-box">
        <div>
          <span>推荐人</span>
          <el-input v-model="searchForm.tjName" clearable placeholder="请输入推荐人姓名" />
        </div>
        <div style="width: 400px !important;">
          <span>创建时间</span>
          <el-date-picker v-model="searchForm.date" type="datetimerange" range-separator="至" start-placeholder="开始时间"
            end-placeholder="结束时间" />
        </div>
        <el-button type="primary" @click="searchHandle">搜索</el-button>
      </div>
      <div class="table-box">
        <el-table :height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
          :cell-style="{fontSize:'14px',color:'#7b7b7d'}" :data="tableData" stripe>
          <el-table-column type="index" width="55" label="序号" align="center" />
          <el-table-column prop="tjName" label="推荐人" align="center">
            <template v-slot="scope">
              {{ scope.row.tjName || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="tjUnique" label="推荐人编码" align="center">
            <template v-slot="scope">
              {{ scope.row.tjUnique || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="tjPhone" label="联系方式" align="center">
            <template v-slot="scope">
              {{ scope.row.tjPhone || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="tjRemark" label="备注" align="center">
            <template v-slot="scope">
              {{ scope.row.tjRemark || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" align="center" width="170">
            <template v-slot="scope">
              {{ scope.row.createTime ? getDateTime(scope.row.createTime)[6] : '' }}
            </template>
          </el-table-column>
          <el-table-column prop="operations" label="操作" align="center" width="160">
            <template v-slot="scope">
              <el-link style="font-size:14px;" type="primary" @click="addAccount(2,scope.row)">编辑</el-link>
              <el-link style="font-size:14px;margin-left: 10px;" type="danger" @click="setHandle(scope.row)">删除</el-link>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-footer">
          <el-pagination style="margin-left: auto;" v-model:currentPage="page" v-model:page-size="size"
            :page-sizes="[10, 20, 50, 100]" small :background="true" layout="total, sizes, prev, pager, next, jumper"
            :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
        </div>
      </div>
    </div>
  </div>
  <replace ref="replaceRef" @dialogClose="getTableData" />
  <confirm-pass ref="confirmPassRef" @verification="deleteHandle" />
</template>

<script>
import { onMounted, reactive, ref, toRefs } from 'vue'
import { getWindowWidthAndHeight, warningDeal, getDateTime } from '@/utils/utils'
import replace from './components/replace.vue'
import confirmPass from '@/components/confirmPass.vue'
import api from './api'
export default {
  components: {
    replace,
    confirmPass,
  },
  setup() {
    const replaceRef = ref(null)
    const confirmPassRef = ref(null)

    const state = reactive({
      deleteId: null,
      tableHeight: 0,

      page: 1,
      size: 10,
      total: 0,

      searchForm: {},
      tableData: [],
    })

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
        ...state.searchForm,
        createTimeStart: (state.searchForm.date && state.searchForm.date.length > 0) ? state.searchForm.date[0].getTime() : null,
        createTimeEnd: (state.searchForm.date && state.searchForm.date.length > 0) ? state.searchForm.date[1].getTime() : null,
      }
      api.accountList(obj).then(res => {
        if (res.code == '200') {
          state.tableData = res.data?.list
          state.total = res.data?.total
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    function addAccount(type, data = {}) {
      replaceRef.value.dialogShow(type, data)
    }

    function setHandle(data) {
      state.deleteId = data.tjUnique
      const title = '删除提示'
      const msg = '是否确定删除该推荐人？'
      confirmPassRef.value.dialogShow(title, msg)
    }

    function deleteHandle() {
      const obj = {
        unique: state.deleteId
      }
      api.accountRemove(obj).then(res => {
        if (res.code == '200') {
          getTableData()
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function searchHandle() {
      state.page = 1
      getTableData()
    }

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 280
      getTableData()
    })

    return {
      ...toRefs(state),
      replaceRef,
      confirmPassRef,
      getTableData,
      addAccount,
      setHandle,
      deleteHandle,
      getDateTime,
      handleSizeChange,
      handleCurrentChange,
      searchHandle,
    }
  }
}
</script>

<style lang="scss" scoped>
.box-all {
  width: 100%;
  height: 100%;
  overflow: hidden;
  .content-box {
    width: 100%;
    height: 100%;
    overflow-y: auto;
    box-sizing: border-box;
    padding: 20px;
  }
}
.table-box {
  margin-top: 20px;
}
.title-box {
  display: flex;
  align-items: center;
  > span {
    font-size: 16px;
    font-weight: bold;
  }
}
.table-footer {
  margin-top: 20px;
  display: flex;
  align-items: center;
}

.search-box {
  display: flex;
  align-items: center;
  gap: 20px;
  margin-top: 10px;
  > div {
    width: 300px;
    display: flex;
    align-items: center;
    font-size: 14px;
    gap: 10px;
    > span {
      flex-shrink: 0;
    }
  }
}
</style>